package edu.ctbu.labtest.service.impl;

import edu.ctbu.labtest.dao.StudentRepository;
import edu.ctbu.labtest.domain.Student;
import edu.ctbu.labtest.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentImplService implements StudentService {

    @Autowired
    StudentRepository studentRepository;

    /**
     * 读取所有的学生数据
     * @return
     */
    public List<Student> findAll(){
        return studentRepository.findAll();
    }

    /**
     * 只支持分页的查询
     * @param pageable
     * @return
     */
    public Page<Student> findAll(Pageable pageable){
        return studentRepository.findAll(pageable);
    }

    /**
     * f分页，带过滤的查询
     * @param student
     * @param pageable
     * @return
     */
    public Page<Student> findAll(Example<Student> student,Pageable pageable){
        return studentRepository.findAll(student,pageable);
    }

    /**
     * 插入学生
     * @param student
     * @return
     */
    public Student insert(Student student){
        return studentRepository.save(student);
    }

    /**
     * 更新学生
     * @param student
     * @return
     */
    public Student update(Student student){
        return studentRepository.save(student);
    }

    /**
     * 删除学生
     * @param student
     * @return
     */
    public void delete(Student student){
        studentRepository.delete(student);
    }

    /**
     * 删除学生
     * @param id
     * @return
     */
    public void delete(Long id){
        Student student = new Student();
        student.setId(id);
        studentRepository.delete(student);
    }

    /**
     * 通过Id获取学生
     * @param id
     * @return
     */
    public Student getById(Long id){
        Student student = studentRepository.findById(id).orElse(null);
        return student;
    }

    /**
     * 按姓名查询
     * @param name
     * @return
     * */
    public List<Student> findByName(String name){
        return studentRepository.findByName(name);
    }

    /**
     * 匹配姓名密码
     * @param name
     * @param password
     * @return
     */
    public List<Student> findByNameAndPassword(String name, String password){
        return studentRepository.findByNameAndPassword(name,password);
    }
}
